package com.tboss.template.system.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.tboss.global.utils.useful.SecurityUtils;
import com.tboss.template.common.dto.NavigationDto;
import com.tboss.template.common.utils.DateUtils;
import com.tboss.template.system.dao.RoleDao;
import com.tboss.template.system.dto.RoleDto;

@Service
public class RoleService {
	
	@Autowired SystemService systemService;
	@Autowired RoleDao roleDao;
	
	private Integer getRoleListCnt(RoleDto dto){
		return this.roleDao.getRoleListCnt(dto);
	}
	
	private List<RoleDto> getRoleList(RoleDto dto){
		return this.roleDao.getRoleList(dto);
	}
	
	public void setModelRoleList(Integer reqPage, Model model, RoleDto dto){
		if(reqPage != null) dto.setCurrentPage(reqPage);
		
		Integer rowsCnt = this.getRoleListCnt(dto);
		NavigationDto pageNavi = new NavigationDto(rowsCnt, dto.getRowPerPage(), dto.getCurrentPage());
		
		model.addAttribute("sysBase", this.systemService.getSysBase(dto));
		model.addAttribute("pageNavi", pageNavi);
		model.addAttribute("roleList", this.getRoleList(dto));
	}
	
	public void roleRegForm(Model model){

	}
	
	public void roleRegForm(Model model, String roleId){
		model.addAttribute("roleInfo", this.roleDao.getRoleInfo(roleId));
	}
	
	public void roleGroups(Model model, String roleId){
		model.addAttribute("grpList", this.roleDao.getRoleGroups(roleId));
	}
	
	public void addRoleInfo(RoleDto dto){
		dto.setRoleRegId(SecurityUtils.getCurrentUserName());
		dto.setRoleRegDate(DateUtils.getDateFormat("yyyyMMddHHmmss"));
		this.roleDao.addRoleInfo(dto);
	}
	
	public void updateRoleInfo(RoleDto dto){
		dto.setRoleModId(SecurityUtils.getCurrentUserName());
		dto.setRoleModDate(DateUtils.getDateFormat("yyyyMMddHHmmss"));
		this.roleDao.updateRoleInfo(dto);
	}

}
